<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
  <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
  <!--<script src="http://{$Think.config.google_map_url}" async="async"></script>-->
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
  <style>
    .layui-upload-img{
      width: 100px;
    }
    .layui-table th{
      padding: 9px 6px;
    }
    .tabs{

      /*border: 1px solid #868686;*/
      padding: 5px 40px;

    }
    #right{
      width: 200px;
      height: 300px;
      border: 1px solid #777;
      overflow-y:scroll;
    }

    .btns span{
      margin: 5px 10px;
    }
    .shi{
      text-align: center;
      font-size: 16px;
      margin: 10px 0;
    }
    .activity{
      background: #1E9FFF;
      color: white;
    }
  </style>
</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="admin">

  <input name="mk_id" id="mk_id"  value="{$dat.mk_id}" placeholder="请输入活动名称" autocomplete="off" class="layui-input"  type="hidden">

  <div class="layui-form-item " id="">
    <label class="layui-form-label">活动名称</label>
    <div class="layui-input-inline" style="width: 500px">
      <input name="mk_name" id="mk_name"  value="{notempty name='dat.mk_name'}{$dat.mk_name}{/notempty}" placeholder="请输入活动名称" autocomplete="off" class="layui-input"  type="text">
    </div>
  </div>


    <div class="layui-form-item">
      <label class="layui-form-label">起始日期</label>
      <div class="layui-input-inline" style="width: 500px">
        <div class="layui-col-xs5">
          <input name="mk_starttime"  value="{$dat.mk_starttime|date='Y-m-d',###}"  id="mk_starttime"  placeholder="请输入开始时间" autocomplete="off" class="layui-input" type="text">
        </div>
        <div class="layui-col-xs1" style="height: 38px;line-height: 38px;text-align: center">至</div>
        <div class="layui-col-xs5">
          <input name="mk_endtime"  value="{$dat.mk_endtime|date='Y-m-d',###}"  id="mk_endtime"  placeholder="请输入开始时间" autocomplete="off" class="layui-input" type="text">
        </div>
      </div>
    </div>



  <div class="layui-form-item " >
    <label class="layui-form-label">已有商品</label>
    <div class="layui-input-inline" style="position: relative;z-index: 10000;width: 400px">
      <select  id="trip" name="goods_id"  lay-filter="addmk"   lay-verify="required" lay-search>
        <option value="999999">请选择商品</option>
        {volist name="trip" id="vo"}
        <option  <?php if($vo['goods_id'] == $dat['goods_id']){ echo 'selected';}?> value="{$vo.goods_id}">{$vo.goods_name}</option>
        {/volist}
      </select>
    </div>
  </div>

  <div  id="classifybox">
    <div class="rongqi">

      {volist name="dat.specs" id="vo"}

      <?php if($vo['specs_types']!='1' || $vo['specs_status']!='2'){
      ?>
      <div class="layui-form-item classifychild" >
        <label class="layui-form-label">套餐规格</label>
        <div class="layui-input-inline person" style="width: 600px">
          <input type="hidden" name="specs_id" id="specs_id" value="{$vo.specs_id}">
          <div class="layui-input-inline" style="width: 400px;padding-bottom: 15px">

            <input name="specs_name" disabled value="{$vo.specs_name}"  placeholder="规格名称" autocomplete="off" class="layui-input" type="text" lay-key="1">
          </div>

          {volist name="vo.person" id="vo1"}
          <div class="layui-form-item personchild">
            <input type="hidden" name="person_id" id="person_id" value="{$vo1.person_id}">

            <div class=" layui-col-xs3">
              <input name="person_name" disabled value="{$vo1.person_name}"  placeholder="出行人群" autocomplete="off" class="layui-input " type="text" lay-key="2">
            </div>
            <div class=" layui-col-xs3">
              <div class="" style="text-align: center; line-height: 38px; float: left">~</div>
              <input name="person_price" value="{$vo1.person_price}" style="float: left;width: 90%"  placeholder="价格" autocomplete="off" class="layui-input " type="number" lay-key="2">
            </div>
            <div class=" layui-col-xs3">
              <div class="" style="text-align: center; line-height: 38px;float: left">~</div>
              <input name="person_num" value="{$vo1.person_num}"  style="float: left;width: 90%" placeholder="数量" autocomplete="off" class="layui-input " type="number" lay-key="2">
            </div>
            <div class=" layui-col-xs3">

              <div class="" style="text-align: center; line-height: 38px;float: left">~</div>
              <input name="minorder_num" value="{$vo1.minorder_num}" style="float: left;width: 90%" placeholder="最低下单数" autocomplete="off" class="layui-input " type="number" lay-key="2">
            </div>
          </div>
          {/volist}

        </div>
        <!--<span  class="layui-btn  layui-btn-sm addperson" style="margin-top: 4px">新增</span>-->

        <!--<span  class="layui-btn layui-btn-danger layui-btn-sm delclassify" id="{$vo.specs_id}" style="margin-top: 4px">删除<i class="layui-icon"></i></span>-->
      </div>

      <?php }?>
      {/volist}

    </div>

  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>

      <!--<button type="button" onclick="location.reload()" class="layui-btn layui-btn-primary">刷新当前页</button>-->
    </div>
  </div>

</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>


    $("#addclassify").on("click",function () {

        var html='    <div class="layui-form-item classifychild" >\n' +
            '      <label class="layui-form-label">套餐规格</label>\n' +
            '      <div class="layui-input-inline person" style="width: 400px">\n' +
            '\n' +
            '        <div class="layui-input-inline" style="width: 400px;padding-bottom: 15px">\n' +
            '          <input name="specs_name" disabled value=""  placeholder="规格名称" autocomplete="off" class="layui-input" type="text" lay-key="1">\n' +
            '        </div>\n' +
            '          </div>\n' +
            // '      <span  class="layui-btn  layui-btn-sm addperson" style="margin-top: 4px;float: left">新增</span>\n' +
            '\n' +
            // '      <span  class="layui-btn layui-btn-danger layui-btn-sm delclassify" style="margin-top: 4px;float: left">删除<i class="layui-icon"></i></span>\n' +
            '    </div>';
        $("#addclassify").parent().before(html);
    });





    $(document).on("click",".delclassify",function () {
        var child=$(this).parent(".classifychild");
        child.remove();
    })

    $(document).on("click",".delperson",function () {
        var child=$(this).parent().parent();
        child.remove();
    })

    $(document).on("click",".addperson",function () {
        var html='        <div class="layui-form-item personchild">\n' +
            '          <div class=" layui-col-xs2">\n' +
            '            <input name="person_name" disabled value=""  placeholder="出行人群" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">\n' +
            '          </div>\n' +
            '          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>\n' +
            '          <div class=" layui-col-xs1">\n' +
            '            <input name="person_price" value=""  placeholder="价格" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">\n' +
            '          </div>\n' +
            '          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>\n' +
            '          <div class=" layui-col-xs1">\n' +
            '            <input name="person_num" value=""  placeholder="数量" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">\n' +
            '          </div>\n' +
            '          <div class=" layui-col-xs2">\n' +
            '            <input name="minorder_num" value=""  placeholder="最低下单数" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">\n' +
            '          </div>\n' +
            '          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>\n' +
            // '          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>\n' +
            '          <div class=" layui-col-xs2">\n' +
            // '            <span  class="layui-btn layui-btn-danger layui-btn-sm delperson" style="margin-top: 4px">删除<i class="layui-icon"></i></span>\n' +
            '          </div>\n' +
            '        </div>';
        $(this).prev().append(html);
    })



    layui.use(['layer', 'form','laydate'], function() {
        // var laydate = layui.laydate;
        var layer = layui.layer,
            $ = layui.jquery,
            laydate= layui.laydate,
            form = layui.form;

        laydate.render({
            elem: '#mk_starttime' //指定元素
        });
        laydate.render({
            elem: '#mk_endtime' //指定元素
        });
        laydate.render({
            elem: '#dates_time', //指定元素
            // position: 'static',
            show: false,
            // position: 'static',
            //     change: function(value, date){ //监听日期被切换
            //     lay('#testView').html(value);
            // },
            done: function(value, date,endDate){
                // console.log(value);
                var span='<p class="shi">'+value+'' +
                    '<input type="hidden" value="'+value+'" name="R_date" >' +
                    '</p>'
                $('#right').append(span);
            }
        });
        //点击事件添加样式
        $(document).on('click','.shi',function () {
            if ($(this).hasClass('activity')){
                $(this).removeClass('activity')
            }else {
                $(this).addClass('activity')
                $(this).siblings().removeClass('activity')
            }


        })
        //删除选中向
        $(document).on('click','#delete',function () {

            $('.activity').remove()
        })

        //清空
        // children()

        $(document).on('click','#empty',function () {

            $('#right').children().remove()
        })


        //判断周和时间段
        form.on('radio(erweima)', function (data) {
            // console.log(data.value);
            if (data.value==1){
                $('#week').css({'display':'none'})
                $('#dates').css({'display':'block'})
            } else {
                $('#week').css({'display':'block'})
                $('#dates').css({'display':'none'})
            }
            // layer.tips('开关checked：' + (this.checked ? 'true' : 'false'), data.othis);
        });
        //判断周的复选框
        form.on("checkbox(allCheck)", function(data){

            if (data.elem.checked) {
                //动态设置全选按钮颜色，不可以这里设置，这里设置后，前端选然后不会有效果的，
                //猜测原因是，form.render("checkbox"); 导致的，设置后layui又渲染了，把我自己设置的颜色覆盖了。所以设置需要在渲染后再设置，就等于是用我的css覆盖了layui的css
                $(".campus").each(function(){
                    $(this).prop('checked', true);
                });
            } else {
                $(".campus").each(function(){
                    $(this).prop('checked',  false);
                });
            }
            form.render("checkbox");
            //渲染后设置我的颜色
            allCheckbox();
        });

        //查看是否被全选了，全选了，全选按钮编辑的时候就是被选中中状态
        function initselect(){
            let allSelect = true;
            $(".campus").each(function(index, elem){
                //每个checkbox添加点击事件，如果点击了，使得所有的按钮中出现了不被选中的，那么全选按钮就不被选中
                if($(this).prop('checked') == false){
                    allSelect = false;
                }
            });
            // console.log("是否全选",allSelect)
            $("#qx").prop('checked',allSelect);
            form.render("checkbox");
            //记得把设置事件放到渲染事件后
            allCheckbox();
        }
        initselect();

        //周点击事件，如果有校区没有被选中，那么全选按钮就不能够显示选中状态
        form.on("checkbox(campus)", function(data){
            let checked = data.elem.checked;
            initselect();
        });


        //商品范围选择下拉添加事件
        form.on('select(addmk)', function(data){
            $('.classifychild').remove();
            $('.personchild').remove();
            //var tripid=data.value;
            var goods_id=data.value;
            if(goods_id=='999999'){
                layer.alert('没有选择商品!')

                return false;
            }else {
                $.post("{:url('xiala')}",{'goods_id':goods_id},function (date) {
                    var data=JSON.parse(date)

                    $('#classifybox').append(muban(data))
                })
            }
        });

        function muban(data){
            var str='';
            $.each(data ,function (k,varl) {

                str+='<div class="layui-form-item classifychild" >';
                str+='    <label class="layui-form-label">套餐规格</label>';
                str+='     <div class="layui-input-inline person" style="width: 600px">';
                str+='         <div class="layui-input-inline" style="width: 400px;padding-bottom: 15px">';
                str+='            <input type="hidden" id="specs_id" name="specs_id" value="'+varl.specs_id+'">';
                str+='            <input name="specs_name" disabled value="'+varl.specs_name+'"  placeholder="规格名称" autocomplete="off" class="layui-input" type="text" lay-key="1">';
                str+='        </div>';
                str+='    </div>';
                if (varl.person!==undefined){
                    $.each(varl.person,function (i,v) {
                        str+='     <div class="layui-form-item personchild">';
                        str+='<div style="padding-right: 40px">'
                        str+='         <div class="layui-col-xs1" style="text-align: center; line-height: 38px">&nbsp;</div>';
                          str+='        <div class=" layui-col-xs2">';
                          str+='          <input type="hidden" id="person_id" name="person_id" value="'+v['person_id']+'">';
                          str+='          <input name="person_name" disabled value="'+v['person_name']+'"  placeholder="出行人群" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">';
                          str+='        </div>';
                        str+='         <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>';
                          str+='          <div class=" layui-col-xs2">';
                          str+='            <input name="person_price" value="'+v['person_price']+'"  placeholder="价格" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">';
                          str+='          </div>';
                        str+='          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>';
                          str+='          <div class=" layui-col-xs2">';
                          str+='            <input name="person_num" value="'+v['person_num']+'"  placeholder="数量" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">';
                          str+='          </div>';
                        str+='          <div class="layui-col-xs1" style="text-align: center; line-height: 38px">~</div>';
                          str+='          <div class=" layui-col-xs2">\n' ;
                          str+='            <input name="minorder_num" value=""  placeholder="最低下单数" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2">\n'
                          str+='          </div>\n'
                        str+='          </div>\n'
                        str+= '     </div>';
                    })
                }
                str+=' </div>';
            })
            return str;

        }

        $(document).on("click",'.deltripspan',function () {
            var dataid=$(this).attr("data-id");
            var tripids=$("#tripids").val();
            var ids=tripids.split(",");
            var newid="";
            for(var i=0 ; i < ids.length; i++){
                if(ids[i]&&dataid!=ids[i]){
                    if(!newid){
                        newid+=ids[i];
                    }else{
                        newid+=","+ids[i];
                    }
                }
            }
            $("#tripids").val(newid);
            $(this).remove();
        })



        form.on('submit(admin)', function (data) {

            var id=$("#classifyid").val();//商品ID
            var data=[];
            var classifychild=$(".classifychild");

            //获取周或日期
            var w = $("input[name='weeks']:checked").val();


            var chs   = [],
                cycle = '',
                T     = [],
                mk_starttime = '',
                mk_endtime  = ''

            if (w == 0){
                $('input[name="week"]:checked').each(function (i,v) {
                    chs.push(v.value)
                });
                cycle = chs.join(',');

                mk_starttime =$('#mk_starttime').val();

                mk_endtime =$('#mk_endtime').val();
            }else{
                $('input[name="R_date"]').each(function (i,v) {
                    T.push(v.value)
                });
                cycle = T.join(',');
            }



            // console.log(w);
            //
            // return false;

            // console.log(classifychild)
            for(var i=0;i<classifychild.length;i++){
                var datas={};
                var datal=[];

                var personchild=$(classifychild[i]).find(".personchild");

                for(var k=0;k<personchild.length;k++){
                    var datass={};
                    var person_id=$(personchild[k]).find("input[name=person_id]").val();
                    if(person_id){
                        datass.person_id=person_id;
                    }
                    datass.person_name=$(personchild[k]).find("input[name=person_name]").val();
                    datass.person_price=$(personchild[k]).find("input[name=person_price]").val();
                    datass.person_num=$(personchild[k]).find("input[name=person_num]").val();
                    datass.minorder_num=$(personchild[k]).find("input[name=minorder_num]").val();
                    datal.push(datass);

                }
                // console.log(datal);
                // var  specs_id =$('#specs_id').val();
                datas.specs_name=$(classifychild[i]).find("input[name=specs_name]").val();
                var  specs_id=$(classifychild[i]).find("input[name=specs_id]").val();
                if(specs_id){
                    datas.specs_id=specs_id;
                }
                datas.persondata=datal;

                data.push(datas);

            }


            console.log(data)

            var  mk_name =$('#mk_name').val();
            var  goods_id =$('#trip option:selected').val();
            var  mk_id =$('#mk_id').val();

            $.ajax({
                url: "{:url('admin/Marketing/edit')}",
                data: {
                    mk_id:mk_id,
                    mk_name:mk_name,
                    goods_id:goods_id,
                    mk_starttime:mk_starttime,
                    mk_endtime:mk_endtime,
                    weeks:w,
                    cycle:cycle,
                    data:data
                },
                type: 'post',
                async: false,
                success: function (res) {
                    layer.msg(res.msg);
                    if (res.code == 1) {
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }, 1500)
                    }
                }
            })
            return false;
        })
    });

    //全选按钮和其他按钮的颜色不一样
    function allCheckbox(){
        qx1=$('#qx').next('div').children('span');
        if($('#qx').prop('checked')){
            //被选中就设置颜色
            qx1.css({
                'background-color':'#e4393c'
            })
        }
    }
    //初始化设置全选按钮的颜色，
    allCheckbox();


</script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('introduce');
    var ue2 = UE.getEditor('introduce2');
    var ue3 = UE.getEditor('introduce3');
    var ue4 = UE.getEditor('introduce4');
    var ue5 = UE.getEditor('introduce5');
</script>
</body>
</html>
